#pragma once

#include "dsegen.h"
#include "RegionObject.h"
#include "FindNeighbor.h"
#include "BlockChessboard.h"

namespace dhis
{
	//------------------------------------------------------------------------------
	typedef struct tagCMacroBlock
	{
		int                            m_key;
		REAL                        m_minDissim;
		REAL                        m_avgDissim;
		ObjectMap              m_objects;
		std::vector<int>       m_mergedObjs;
	} CMacroBlock;

	//------------------------------------------------------------------------------
	typedef std::hash_map<int,CMacroBlock*> BlockMap;

	//------------------------------------------------------------------------------
	void _createMacroBlockMap(CBlockChessboard& chessboard,BlockMap& blocks);

	//------------------------------------------------------------------------------
	void _deleteMacroBlockMap(BlockMap& blocks);

	//------------------------------------------------------------------------------
	void _computeStatistics(CMacroBlock* block);
	
	//------------------------------------------------------------------------------
	void _computeStatistics(CBlockChessboard& chessboard,BlockMap& blocks,REAL&dAvgDissim,REAL&dMinDissim,int& nObjCount);

	//------------------------------------------------------------------------------
	void _computeStatistics(std::vector<CMacroBlock*>& blocks,REAL&dAvgDissim,REAL&dMinDissim,REAL&dMaxDissim,int& nObjCount);

	//------------------------------------------------------------------------------
	bool _insertObject(CBlockChessboard& chessboard,BlockMap& blocks,CRegionObject* obj);

	//------------------------------------------------------------------------------
	bool _hasOutNeighborObject(CMacroBlock* block,CRegionObject* obj);

	//------------------------------------------------------------------------------
	void _computeNeighborHeterogeneity(CBlockChessboard& chessboard,BlockMap& blocks,const CImageInfo* pImg,const CParameter* pPara);

	//------------------------------------------------------------------------------
	void _initMacroBlocks(std::vector<CMacroBlock*>& blocks);
	void _destroyMacroBlocks(std::vector<CMacroBlock*>& blocks);
	
	

	//------------------------------------------------------------------------------
	bool _findObjectById(std::vector<CMacroBlock*>& blocks,int objId,ObjectMap::iterator& pos,int& blockId);

	//------------------------------------------------------------------------------
	bool _exportObjectsToShapeFile(std::vector<CMacroBlock*>& blocks,const std::string& szFolder);
	bool _exportObjectsToShapeFile(CMacroBlock* block,const std::string& szFileName);

	bool _exportObjectsToShapeFile2(CMacroBlock* block,const std::string& szFileName);

	//------------------------------------------------------------------------------
	bool _exportFrontierObjectsToShapeFile(std::vector<CMacroBlock*>& blocks,const std::string& szFolder);
	bool _exportFrontierObjectsToShapeFile(CMacroBlock* block,const std::string& szFileName);

	
	//------------------------------------------------------------------------------
	bool _exportObjectsToShapeFile(CImageInfo* img,ObjectMap& objs,const string& szShpFile);

	//------------------------------------------------------------------------------
	OGRLayer* _createShapeLayer(OGRDataSource *poDS,CImageInfo* pImg,const std::string& szShpFile);
	bool _exportShapeFile(CMacroBlock* block,CImageInfo* pImg,OGRLayer* poLayer);
	bool _exportShapeFile(std::vector<CMacroBlock*>& blocks,const std::string& szFolder);

}

